<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataBidLifeCycleService;
use common\utils\DateTimeUtils;

class BidLifeCycleController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     *
     * @api {get} /bigdata/bid-life-cycle/get-list 招标合并，生命周期
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 招标合并
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} area_code                 项目地区
     * @apiParam {String} type                      公告类型
     * @apiParam {String} type                      公告类型
     * @apiParam {String} data_source               招标类型
     *
     * @apiParam {String} company_area_code         企业地区
     * @apiParam {String} company_type              企业类型
     * @apiParam {String} company_label             企业标签
     * @apiParam {Number} page                      页码
     * @apiParam {Number} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     */
    public function actionGetList()
    {
        try {
            $id = $this->getParam('id');
            $subId = $this->getParam('sub_id');
            $searchType = $this->getParam('search_type');//搜索类型：空、标题、招标单位、中标单位
            $keyword = $this->getParam('keyword');
            $areaCode = $this->getParam('area_code');//招标地区，多个用,分隔
            $type = $this->getParam('type');//公告类型，多个用,分隔
            $dataSourceCode = $this->getParam('data_source_code');//招标类型，多个用,分隔
            $publishDateStart = $this->getParam('publish_date_start');//发布时间
            $publishDateEnd = $this->getParam('publish_date_end');
            $endDateStart = $this->getParam('end_date_start');//截止日期
            $endDateEnd = $this->getParam('end_date_end');
            $companyAreaCode = $this->getParam('company_area_code');//企业地区，多个用,分隔
            $companyType = $this->getParam('company_type');//企业类型，多个用,分隔
            $companyLabel = $this->getParam('company_label');//企业标签，多个用,分隔
            $organizingCode = $this->getParam('organizing_code');//企业信用代码，多个用,分隔
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $areaCode = array_filter(explode(",", $areaCode));
            $type = array_filter(explode(",", $type));
            $dataSourceCode = array_filter(explode(",", $dataSourceCode));
            $companyAreaCode = array_filter(explode(",", $companyAreaCode));
            $companyType = array_filter(explode(",", $companyType));
            $companyLabel = array_filter(explode(",", $companyLabel));
            $organizingCode = array_filter(explode(",", $organizingCode));
            $searchType = array_filter(explode(",", $searchType));

            //如果所有过滤条件为空，则默认只取发布时间为近2年的数据（解决数据量过大的性能问题）
            if (empty($keyword) && count($areaCode) >= 402 && empty($companyAreaCode) && empty($dataSourceCode) && empty($companyType) && empty($companyLabel) && empty($publishDateStart)
                && empty($publishDateEnd) && empty($endDateStart) && empty($endDateEnd) && empty($type)
            ) {
                $publishDateStart = DateTimeUtils::lastYear(2);
                $publishDateEnd = DateTimeUtils::date();
            }

            $bigDataService = new BigDataBidLifeCycleService();
            $rs = $bigDataService->getList(
                $keyword,
                $searchType,
                $id,
                $subId,
                $areaCode,
                $type,
                $dataSourceCode,
                $publishDateStart,
                $publishDateEnd,
                $endDateStart,
                $endDateEnd,
                $companyAreaCode,
                $companyType,
                $companyLabel,
                $organizingCode,
                $page,
                $pageSize
            );
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    public function actionFuzzySearch()
    {
        try {
            $keyword = $this->getParam('keyword');
            $searchType = $this->getParam('search_type');//搜索类型：空、标题、招标单位、中标单位
            $searchType = array_filter(explode(",", $searchType));
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $bigDataService = new BigDataBidLifeCycleService();
            $rs = $bigDataService->guessName($keyword, $searchType, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }
}
